package site.eblog.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import site.eblog.entity.User;
import site.eblog.service.UserService;

@Controller
public class eBlogController {
	
	@Autowired UserService userService;
	
	@RequestMapping("mgindex")
	public String mgIndex(){
		return "mgindex";
	}
	
	@RequestMapping("mgmain")
	public String mgMain(){
		return "mgmain";
	}
	@RequestMapping("userinfo")
	public String userInfo(){
		return "userinfo";
	}
	@RequestMapping("editpw")
	public String editPW(){
		return "editPW";
	}
	@RequestMapping("userpic")
	public String userPic(){
		return "userpic";
	}
	@RequestMapping("coverpic")
	public String coverPic(){
		return "coverpic";
	}
	@RequestMapping("mgusers")
	public String mgUsers(){
		return "mgusers";
	}
	@RequestMapping("mgtags")
	public String mgTags(){
		return "mgtags";
	}
	@RequestMapping("addtag")
	public String addTag(){
		return "addtag";
	}
	@RequestMapping("myarticle")
	public String myArticle(){
		return "myarticle";
	}
	@RequestMapping("addarticle")
	public String addArticle(){
		return "addarticle";
	}
	@RequestMapping("editarticle")
	public String editArticle(HttpServletRequest request){
		String eidtArticleId = request.getParameter("id");
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		session.setAttribute("eidtArticleId", eidtArticleId);
		return "editarticle";
	}
	@RequestMapping("mgarticle")
	public String mgArticle(){
		return "mgarticle";
	}
	@RequestMapping("/")
	public String index(){
		return "index";
	}
	@RequestMapping("content")
	public String content(HttpServletRequest request){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		String contentId = request.getParameter("id");
		session.setAttribute("contentId", contentId);
		return "content";
	}
	@RequestMapping("list")
	public String list(HttpServletRequest request){
		String listTag = request.getParameter("tag");
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		session.setAttribute("listTag", listTag);
		return "list";
	}
	@RequestMapping("center")
	public String center(HttpServletRequest request){
		String author = request.getParameter("author");
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		session.setAttribute("centerAuthor",author);
		return "center";
	}
	@RequestMapping("searchPage")
	@ResponseBody
	public String search(HttpServletRequest request){
		String searchText = request.getParameter("searchText");
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		session.setAttribute("searchText",searchText);
		return null;
	}
	@RequestMapping("search")
	public String searchPage(HttpServletRequest request){
		return "search";
	}
	
	@RequestMapping("mgindexgetuser")
	@ResponseBody
	public User mgIndexGetUser(){
		Subject currentUser = SecurityUtils.getSubject();
		String userName = (String)currentUser.getPrincipal();
		User user = userService.findByName(userName);
		user.setPassword(null);
		return user;
	}
}
